package com.cbwl.eoas.cms.web.controller.sysconfig;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cbwl.eoas.cms.api.sysconfig.ISysUserService;
import com.cbwl.eoas.cms.model.sysconfig.SysUserDTO;
import com.cbwl.eoas.common.framework.session.SessionManager;
import com.cbwl.eoas.common.framework.vo.PageRequest;
import com.cbwl.eoas.common.util.Md5Util;
import com.cbwl.eoas.common.util.constain.DicDetailConstant;
import com.cbwl.eoas.common.web.Result;
import com.github.pagehelper.PageInfo;



/**
 * 
 * <p>[描述信息：用户管理Controller]</p>
 *
 * @author 虞云波 - yuyunbo@3856.cc
 * @version 1.0 Created on 2017年10月12日 下午13:29:23
 */
@Controller
@RequestMapping("/sysuser")
public class SysUserController {
	
	private static Logger logger = LoggerFactory.getLogger(SysUserController.class);
	
	//业务层注解
	@Autowired
	private ISysUserService sysUserService;
	
	/**
	 * <p>初始化界面</p>
	 * 
	 * @return
	 * @author 虞云波 - yuyunbo@3856.cc
	 * @version 1.0 Created on 2017年10月12日 下午13:29:23
	 */
	@RequestMapping("/initSysUser")
	public String initSysUserPage() {
		return "initSysUser";
	}


	/**
	 * <p>初始化新增修改界面</p>
	 * 
	 * @return
	 * @author 虞云波 - yuyunbo@3856.cc
	 * @version 1.0 Created on 2017年10月12日 下午13:29:23
	 */
	@RequestMapping("/initAddUpdateSysUserPage")
	public String initAddUpdateSysUserPage(HttpServletRequest request,SysUserDTO bean) {
		
		//职员id不为空
		if(!StringUtils.isEmpty(bean.getEmpId())){
			//关联出userId
			SysUserDTO userBean = sysUserService.getUserDTOByParam(bean);
			if(!StringUtils.isEmpty(userBean)){
				bean.setId(userBean.getId());
			}
		}
		request.setAttribute("bean", bean);
		return "initAddUpdateSysUserPage";
	}
	

	/**
	 * <p>查询（分页）</p>
	 * 
	 * @param bean
	 * @return
	 * @author 虞云波 - yuyunbo@3856.cc
	 * @version 1.0 Created on 2017年10月12日 下午13:29:23
	 */
	@RequestMapping("/querySysUserPage")
	@ResponseBody
	public Object querySysUserPage(SysUserDTO bean,PageRequest pageRequest) {
		Result result = new Result(Result.Status.OK, "查询成功", "200");
		// 分页查询用户信息
		PageInfo<?> userPageInfo =  sysUserService.queryPageList(bean,pageRequest);
		result.setData(userPageInfo);
		return	result;
		
	}

	/**
	 * 
	 * <p>通过id查询主体信息</p>
	 * 
	 * @param bean
	 * @return
	 * @author 虞云波 - yuyunbo@3856.cc
	 * @version 1.0 Created on 2017年10月12日 下午13:29:23
	 */
	@RequestMapping("/getSysUserById")
	@ResponseBody
	public Object getSysUserById(SysUserDTO bean) {
		return sysUserService.getObjectById(bean);
	}

	/**
	 * 
	 * <p>保存主体信息</p>
	 * 
	 * @param bean
	 * @return
	 * @author 虞云波 - yuyunbo@3856.cc
	 * @version 1.0 Created on 2017年10月12日 下午13:29:23
	 */
	@RequestMapping("/saveSysUser")
	@ResponseBody
	public Object saveSysUser(SysUserDTO bean) throws Exception {
		bean.setGmtCreate(new Date());
		bean.setCreater(SessionManager.getCurrentUser().getId());
		bean.setUserLoginname(bean.getUserJobno());
		bean.setUserPassword(Md5Util.encodePassword(bean.getUserPassword(), bean.getUserJobno()));
		bean.setLoginPwdErrorNum(0);
		bean.setOnlineis(0);
		
		bean.setUserStatus(DicDetailConstant.common_state_1);
		
		sysUserService.saveObject(bean);
		logger.info("{}新建了用户信息,用户工号{}", SessionManager.getCurrentUser().getEmpName(),bean.getUserJobno());
		 return bean.getId();
	}

	/**
	 * 
	 * <p>更新主体信息</p>
	 * 
	 * @param bean
	 * @return
	 * @author 虞云波 - yuyunbo@3856.cc
	 * @version 1.0 Created on 2017年10月12日 下午13:29:23
	 */
	@RequestMapping("/updateSysUser")
	@ResponseBody
	public Object updateSysUser(SysUserDTO bean) throws Exception {		
		bean.setGmtModified(new Date());
		bean.setModifier(SessionManager.getCurrentUser().getId());
		logger.info("{}修改了用户信息,用户工号{}", SessionManager.getCurrentUser().getEmpName(),bean.getUserJobno());
		return sysUserService.updateObject(bean);
	}

	
	/**
	 * 
	 * <p>用户重置密码</p>
	 * 
	 * @param bean
	 * @return
	 * @throws Exception
	 * @author: 虞云波 - yuyunbo@3856.cc 
	 * @date: Created on 2017年12月6日 下午2:28:35
	 */
	@RequestMapping("/updateUserRetPassWord")
	@ResponseBody
	public Object updateUserRetPassWord(SysUserDTO bean) throws Exception {		
		bean.setGmtModified(new Date());
		bean.setModifier(SessionManager.getCurrentUser().getId());
		logger.info("{}修改了用户信息,用户工号{}", SessionManager.getCurrentUser().getEmpName(),bean.getUserJobno());
		return sysUserService.updateUserPassWord(bean);
	}
	
	
	
	/**
	 * 
	 * <p>删除主体信息</p>
	 * 
	 * @param bean
	 * @return
	 * @author 虞云波 - yuyunbo@3856.cc 
	 * @version 1.0 Created on 2017年10月12日 下午13:29:23
	 * @throws Exception 
	 */
	@RequestMapping("/deleteSysUser")
	@ResponseBody
	public Object deleteSysUser(SysUserDTO bean) throws Exception {
		logger.info("{}删除了用户信息,用户工号{}", SessionManager.getCurrentUser().getEmpName(),bean.getUserJobno());
		return sysUserService.deleteObject(bean);
	}
}
